package edu.ahpu.propertymanage.controller;

import com.alibaba.fastjson.JSON;
import edu.ahpu.propertymanage.pojo.*;
import edu.ahpu.propertymanage.service.*;
import edu.ahpu.propertymanage.util.ExcelTitleMap;
import edu.ahpu.propertymanage.util.SaveOwner;
import edu.ahpu.propertymanage.util.TimeChange;
import edu.ahpu.propertymanage.util.WriterExcel;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

@RestController
@RequestMapping("/excel")
public class ExcelController {

    @Autowired
    ExcelServer excelServer;

    @Autowired
    WriterExcel writerExcel;

    @Autowired
    ExcelTitleMap excelTitleMap;

    @Autowired
    ManagersService managersService;

    @Autowired
    WorkerService workerService;

    @Autowired
    OwnerService ownerService;

    @Autowired
    VisitorService visitorService;

    @Autowired
    EleCostService eleCostService;

    @Autowired
    WaterCostService waterCostService;

    @Autowired
    GasCostServie gasCostServie;

    @Autowired
    SaveOwner saveOwner;

    @Autowired
    TimeChange timeChange;

    static String ftpPath = "G:\\FileHistory\\";

    @GetMapping("getvisitorexcel")
    public FileSystemResource getvExcel() throws ParseException {
        List<Visitor> list = visitorService.list();
        String fileName = "visitor.xlsx";
        SXSSFWorkbook workbook = excelServer.writerExcel(excelTitleMap.getVisitorTitle(), list);
        boolean bool = writerExcel.writerExcel(fileName, workbook);
        File file = new File(ftpPath + fileName);
        FileSystemResource fileSystemResource = new FileSystemResource(file);
        return fileSystemResource;
    }

    @GetMapping("getworkerexcel")
    public FileSystemResource getwExcel() throws ParseException {
        List<Worker> list = workerService.list();
        String fileName = "worker.xlsx";
        SXSSFWorkbook workbook = excelServer.writerExcel(excelTitleMap.getWorkerTitle(), list);
        boolean bool = writerExcel.writerExcel(fileName, workbook);
        File file = new File(ftpPath + fileName);
        FileSystemResource fileSystemResource = new FileSystemResource(file);
        return fileSystemResource;
    }

    @GetMapping("getownerexcel")
    public FileSystemResource getoExcel() throws ParseException {
        List<Owner> list = ownerService.list();
        String fileName = "owner.xlsx";
        SXSSFWorkbook workbook = excelServer.writerExcel(excelTitleMap.getOwnerTitle(), list);
        boolean bool = writerExcel.writerExcel(fileName, workbook);
        File file = new File(ftpPath + fileName);
        FileSystemResource fileSystemResource = new FileSystemResource(file);
        return fileSystemResource;
    }

    @GetMapping("getmanagerexcel")
    public FileSystemResource getmExcel() throws ParseException {
        List<Managers> list = managersService.list();
        String fileName = "manager.xlsx";
        SXSSFWorkbook workbook = excelServer.writerExcel(excelTitleMap.getManagersTitle(), list);
        boolean bool = writerExcel.writerExcel(fileName, workbook);
        File file = new File(ftpPath + fileName);
        FileSystemResource fileSystemResource = new FileSystemResource(file);
        return fileSystemResource;
    }

    @GetMapping("getwater")
    public FileSystemResource getwaterExcel() throws ParseException {
        List<WaterCost> list = waterCostService.list();
        for (WaterCost w : list) {
            w.setLastMonthWaterTime(w.getThisMonthWaterTime());
            w.setLastMonthWaterCount(w.getThisMonthWaterCount());
            w.setThisMonthWaterTime(timeChange.addAMountTime(w.getLastMonthWaterTime()));
            w.setThisMonthWaterCount(0f);
            w.setUserWaterCount(0f);
        }


        String fileName = "watercost.xlsx";
        SXSSFWorkbook workbook = excelServer.writerExcel(excelTitleMap.getWaterCost(), list);
        boolean bool = writerExcel.writerExcel(fileName, workbook);
        File file = new File(ftpPath + fileName);
        FileSystemResource fileSystemResource = new FileSystemResource(file);
        return fileSystemResource;
    }

    @GetMapping("getele")
    public FileSystemResource geteleExcel() throws ParseException {
        List<EleCost> list = eleCostService.list();
        for (EleCost w : list) {
            w.setLastMonthEleTime(w.getThisMonthEleTime());
            w.setLastMonthEleCount(w.getThisMonthEleCount());
            w.setThisMonthEleTime(timeChange.addAMountTime(w.getLastMonthEleTime()));
            w.setThisMonthEleCount(0f);
            w.setUseEleCount(0f);
        }
        String fileName = "elecost.xlsx";
        SXSSFWorkbook workbook = excelServer.writerExcel(excelTitleMap.getEleCost(), list);
        boolean bool = writerExcel.writerExcel(fileName, workbook);
        File file = new File(ftpPath + fileName);
        FileSystemResource fileSystemResource = new FileSystemResource(file);
        return fileSystemResource;
    }

    @GetMapping("getgas")
    public FileSystemResource getgasExcel() throws ParseException {
        List<GasCost> list = gasCostServie.list();
        for (GasCost w : list) {
            w.setLastMonthGasTime(w.getLastMonthGasTime());
            w.setLastMonthGasCount(w.getThisMonthGasCount());
            w.setThisMonthGasTime(timeChange.addAMountTime(w.getThisMonthGasTime()));
            w.setThisMonthGasCount(0f);
            w.setUseGasCount(0f);
        }
        String fileName = "watercost.xlsx";
        SXSSFWorkbook workbook = excelServer.writerExcel(excelTitleMap.getGasCost(), list);
        boolean bool = writerExcel.writerExcel(fileName, workbook);
        File file = new File(ftpPath + fileName);
        FileSystemResource fileSystemResource = new FileSystemResource(file);
        return fileSystemResource;
    }

    @PostMapping("postManagerExcel")
    public String postExcel(MultipartFile file) throws IOException {
        //文件流
        InputStream fileInputStream = file.getInputStream();
        //标题Map
        LinkedHashMap titleMap = excelTitleMap.getManagersTitle();
        //获取对象List（Map类型）
        List<Object> objects = excelServer.postExcel(fileInputStream, titleMap);
        //转变成ObjectList
        List<Managers> managers = JSON.parseArray(objects.toString(), Managers.class);
        //完善相关信息
        for (Managers m : managers) {
            if (m.getManagerPw() == null)
                m.setManagerPw("123");
        }
        //保存到数据库
        boolean bool = managersService.saveOrUpdateBatch(managers);
        if (bool)
            return "success";
        else
            return "error";
    }

    @PostMapping("postWorkerExcel")
    public String postWorkerExcel(MultipartFile file) throws IOException {
        //文件流
        InputStream fileInputStream = file.getInputStream();
        //标题Map
        LinkedHashMap titleMap = excelTitleMap.getWorkerTitle();
        //获取对象List（Map类型）
        List<Object> objects = excelServer.postExcel(fileInputStream, titleMap);
        //转变成ObjectList
        List<Worker> workers = JSON.parseArray(objects.toString(), Worker.class);
//        完善相关信息
        for (Worker m : workers) {
            if ("".equals(m.getWorkerPw()) || m.getWorkerPw() == null)
                m.setWorkerPw("123");
        }
        //保存到数据库
        boolean bool = workerService.saveOrUpdateBatch(workers);
        if (bool)
            return "success";
        else
            return "error";
    }

    @PostMapping("postOwnerExcel")
    public String postOwnerExcel(MultipartFile file) throws IOException {
        //文件流
        InputStream fileInputStream = file.getInputStream();
        //标题Map
        LinkedHashMap titleMap = excelTitleMap.getOwnerTitle();
        //获取对象List（Map类型）
        List<Object> objects = excelServer.postExcel(fileInputStream, titleMap);
        //转变成ObjectList
        List<Owner> owners = JSON.parseArray(objects.toString(), Owner.class);
        //完善相关信息并保存数据库
        for (Owner owner : owners) {
            System.out.println("******************");
            System.out.println(owner);
            if (saveOwner.saveOwner(owner))
                continue;
            else
                return "error";
        }
        return "success";

    }

    @PostMapping("postwater")
    public String postWater(MultipartFile file) throws IOException {
        //文件流
        InputStream fileInputStream = file.getInputStream();
        //标题Map
        LinkedHashMap titleMap = excelTitleMap.getWaterCost();
        //获取对象List（Map类型）
        List<Object> objects = excelServer.postExcel(fileInputStream, titleMap);
        //转变成ObjectList
        List<WaterCost> list = JSON.parseArray(objects.toString(), WaterCost.class);
        boolean bool = waterCostService.saveOrUpdateBatch(list);
        if (bool)
            return "success";
        else
            return "error";
    }

    @PostMapping("postele")
    public String postEle(MultipartFile file) throws IOException {
        //文件流
        InputStream fileInputStream = file.getInputStream();
        //标题Map
        LinkedHashMap titleMap = excelTitleMap.getEleCost();
        //获取对象List（Map类型）
        List<Object> objects = excelServer.postExcel(fileInputStream, titleMap);
        //转变成ObjectList
        List<EleCost> list = JSON.parseArray(objects.toString(), EleCost.class);
        boolean bool = eleCostService.saveOrUpdateBatch(list);
        if (bool)
            return "success";
        else
            return "error";
    }

    @PostMapping("postgas")
    public String postGas(MultipartFile file) throws IOException {
        //文件流
        InputStream fileInputStream = file.getInputStream();
        //标题Map
        LinkedHashMap titleMap = excelTitleMap.getGasCost();
        //获取对象List（Map类型）
        List<Object> objects = excelServer.postExcel(fileInputStream, titleMap);
        //转变成ObjectList
        List<GasCost> list = JSON.parseArray(objects.toString(), GasCost.class);
        boolean bool = gasCostServie.saveOrUpdateBatch(list);
        if (bool)
            return "success";
        else
            return "error";
    }
}

